package com.lai.note.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lai.note.R;
import com.lai.note.constant.Constant;
import com.lai.note.util.RetrofitRequest;
import com.lai.note.util.ToastUtil;
import com.lai.note.util.ValueUtil;
import com.lai.note.dto.ResponseResult;
import com.lai.note.interceptor.AddCookiesInterceptor;
import com.lai.note.interceptor.ReceivedCookiesInterceptor;
import com.lai.note.service.WenanService;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class WriteWenanFragment extends Fragment {
    public WriteWenanFragment(){
        super(R.layout.fragment_write_wenan);
    }

    private EditText et_wenan;
    private Button btn_save;

    private Retrofit retrofit = null;
    private WenanService wenanService = null;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        et_wenan = getView().findViewById(R.id.et_write_wenan);
        btn_save = getView().findViewById(R.id.btn_save2recommend);
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String wenan = et_wenan.getText().toString();
                if(ValueUtil.isEmpty(wenan)){
                    Toast.makeText(getActivity(),"文案内容不能为空",Toast.LENGTH_SHORT).show();
                }else{

                    retrofit = RetrofitRequest.getInstance(getContext());
                    wenanService = retrofit.create(WenanService.class);
                    Call<ResponseResult> call = wenanService.addNewWenan(wenan);
                    call.enqueue(new Callback<ResponseResult>() {
                        @Override
                        public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                            ResponseResult res = response.body();
                            if(res != null && res.getStatus()==200){
                                ToastUtil.display(getActivity(),"请耐心等待管理员审核^-^\n通过后就能看到自己的文案啦");
                            }else{
                                ToastUtil.display(getActivity(),"-_--__啊哦出现bug啦-_-_-");
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseResult> call, Throwable t) {
                            t.printStackTrace();
                        }
                    });
                }
            }
        });
    }
}
